#Copyright (c) 2009, Adam McKerlie
#All rights reserved.

#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#    * Neither the name of PyNAS  nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#! /usr/bin/python

import sys, string 
from subprocess import Popen,PIPE, STDOUT
import re

#Declare all functions
def check_prereqs():
   
    #CHECK APACH 
    p = Popen("dpkg-query -l apache2", shell=True,stdout=PIPE,stderr=STDOUT)
    result = p.stdout.read()
    if(result.find("No packages found matching test.") != -1):
        install_apache()
    else:
        try:
            #Check the result to see if there is a version ID for PostgreSQL (if there isn't, we need to install it)
            re.search("(\d)\.(\d)\.(\d)", result).group(0)
            print "Apache is installed and ready to go"
        except:
            install_apache()   
 
    #CHECK POSTGRESQL
    p = Popen("dpkg-query -l postgresql", shell=True,stdout=PIPE,stderr=STDOUT)
    result = p.stdout.read()
    
    if(result.find("No packages found matching test.") != -1):
        install_postgre()
    else:
        print "Checking to make sure PostgreSQL is valid..."
        try:
            #Check the result to see if there is a version ID for PostgreSQL (if there isn't, we need to install it)
            re.search("(\d)\.(\d)\.(\d)", result).group(0)
            print "postgreSQL is installed and ready to go"
        except:
            install_postgre()            

    #CHECK psycopg2
    p = Popen("dpkg-query -l python-psycopg2", shell=True,stdout=PIPE, stderr=STDOUT) 
    result = p.stdout.read()

    if(result.find("No packages found matching test.") != -1):
        install_postgre_mod()
    else:
        print "Checking to make sure psycopg is valid..."
        try:
            #Check the result to see if there is a version ID for PostgreSQL (if there isn't, we need to install it)
            re.search("(\d)\.(\d)\.(\d)", result).group(0)
            print "psycopg2 is installed and ready to go"
        except:
            install_postgre_mod()
    

    #CHECK SVN
    p = Popen("dpkg-query -l subversion", shell=True,stdout=PIPE, stderr=STDOUT)
    result = p.stdout.read()

    if(result.find("No packages found matching test.") != -1):
        install_svn()
    else:
        print "Checking to make sure svn valid..."
        try:
            #Check the result to see if there is a version ID for PostgreSQL (if there isn't, we need to install it)
            re.search("(\d)\.(\d)\.(\d)", result).group(0)
            print "svn is installed and ready to go"
        except:
            install_svn()
    
    #CHECK DJANGO 
    try:
        import django
    except:
        install_django() 
    

def install_svn():
    print "Installing svn from Ubuntu repository"
    p = Popen("apt-get -y install subversion", shell=True,stdout=PIPE, stderr=STDOUT)
    print "SVN installed and ready to use..."


def install_apache():
    print "Installing apache2 from Ubuntu repository"
    p = Popen("apt-get -y install apache2", shell=True,stdout=PIPE,stderr=STDOUT)    
    print "Apache installed and ready to use..."
    
def install_postgre():
    print "Installing postgreSQL from Ubuntu repository" 
    p = Popen("apt-get -y install postgresql", shell=True,stdout=PIPE,stderr=STDOUT)
    print "PostgreSQL installed and ready to use..."

def install_postgre_mod():
    print "Installing psycopg2 from Ubuntu repository"
    p = Popen("apt-get -y install python-psycopg2", shell=True,stdout=PIPE,stderr=STDOUT)
    print "psycopg installed and ready to use..."

def install_django():
    print "Installing django from repository"
    p = Popen("svn co http://code.djangoproject.com/svn/django/trunk/ /usr/local/lib/django-trunk", shell=True,stdout=PIPE, stderr=STDOUT)
    p = Popen("python -c \"from distutils.sysconfig import get_python_lib; print get_python_lib()\"", shell=True,stdout=PIPE, stderr=STDOUT)
    result = p.stdout.read()
    cmd = "ln -s /usr/local/lib/django-trunk/django %s/django" % result
    p = Popen(cmd,shell=True,stdout=PIPE, stderr=STDOUT)
    p = Popen("ln -s /usr/local/lib/django-trunk/django/bin/django-admin.py /usr/local/bin",shell=True,stdout=PIPE, stderr=STDOUT)
    
 


def log_err():
    print "log_err"
	

def main():
    check_prereqs()    

     
if __name__ == '__main__':
    main()

